//
// Created by rengen.gong on 2021/12/21.
//

#ifndef LEARN1_STACKTEMPLATE_H
#define LEARN1_STACKTEMPLATE_H

#include "iInvoke.h"
#include "iostream"

using namespace std;


template<typename T, int MAXSIZE>
class MStack {

private:
    T elems[MAXSIZE];
    int top;

public:
    MStack() {}

    void init() {
        top = -1;
    }

    bool isFull() {
        if (top >= MAXSIZE - 1) {
            return true;
        } else {
            return false;
        }
    }

    bool isEmpty() {
        if (top == -1) {
            return true;
        } else {
            return false;
        }
    }

    void push(T e);

    T pop();

};

template<typename T, int MAXSIZE>
void MStack<T, MAXSIZE>::push(T e) {
    if (!isFull()) {
        elems[++top] = e;
    } else {
        cout << "栈已满，请不要再加入元素！" << endl;
    }
}

template<typename T, int MAXSIZE>
T MStack<T, MAXSIZE>::pop() {
    if (!isEmpty()) {
        return elems[top--];
    } else {
        cout << "栈已空，请不要再弹出元素！";
        return 0;
    }
}

class StackTemplate :
        public virtual iInvoke {

public:
    void invoke() override {
        iInvoke::invoke();

        MStack<int,10> s1;
        s1.init();
        s1.push(1);
        s1.push(12);
        s1.push(21);
        s1.push(31);
        s1.push(14);
        s1.push(51);

        for(int i=1;i<11;i++){
            cout << s1.pop() << "\n";
        }
        cout << endl;

    }


};


#endif //LEARN1_STACKTEMPLATE_H
